package com.googlecode.dgwt.client.dijit.form;

import com.google.gwt.dom.client.InputElement;
import com.google.gwt.user.client.ui.Focusable;
import com.google.gwt.user.client.ui.HasName;
import com.google.gwt.user.client.ui.HasText;
import com.googlecode.dgwt.client.dijit.Dijit;
import com.googlecode.dgwt.client.dijit.base.FocusWidget;
import com.googlecode.dgwt.client.dojo.Dojo;
import com.googlecode.dgwt.client.dojo.exception.ValidationException;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public abstract class FormWidget extends FocusWidget implements Focusable, HasText, HasName {

    private boolean required;
    private String requiredMessage = "";
    private String displayLabel;
    private Label labelWidget; // TODO : setar referencia quando criado um label para este componente


    protected FormWidget(boolean ready) {
        super(ready);
    }

    @Override
    protected void initialize() {
        super.initialize();
        setElementForAttribute("type", "inputElement");
        setElementForAttribute("name", "inputElement");
        setElementForAttribute("value", "inputElement");
        setElementForAttribute("readonly", "inputElement");
        setElementForAttribute("autocomplete", "inputElement");
    }

    protected void setInputElement(InputElement element) {
        setAttachedElement("inputElement", element);
    }
    
    @Override
    public String getName() {
        return getAttribute("name");
    }
    
    @Override
    public void setName(String name) {
        setAttribute("name", name);
    }

    public boolean isEmpty(){
        return getText().isEmpty();
    }
    
    public boolean isValid() {
        return doValidation() != null;
    }   
    
    public void validate() throws ValidationException {
        String message = doValidation();
        if (message != null) {
            throw new ValidationException(message);
            // TODO this.setFocus(), exibir mensagem 
        }
    }

    protected String doValidation() {
        String message = null;
        
        if (isRequired() && isEmpty()) {
            message = getDisplayRequiredMessage();
        }

        return message;
    }

    public boolean isRequired() {
        return required;
    }

    public void setRequired(boolean required) {
        this.required = required;
    }

    public String getRequiredMessage() {
        return requiredMessage;
    }

    public void setRequiredMessage(String requiredMessage) {
        if (requiredMessage == null) {
            requiredMessage = "";
        }
        this.requiredMessage = requiredMessage;
    }

    public void setDisplayLabel(String label) {
        displayLabel = label;
    }
    
    public String getDisplayLabel() {
        if (displayLabel != null) {
            return displayLabel;
        }
        if (labelWidget != null) {
            return labelWidget.getText();
        }
        return Dojo.string.toProperCase(getName());
    }

    protected String getDisplayRequiredMessage() {
        if (requiredMessage.isEmpty()) {
            return Dijit.MESSAGE_RESOURCES.valueRequired(getDisplayLabel());
        }
        return Dojo.string.propertiesSubstitute(requiredMessage, getDisplayLabel());
    }

}